package stud.mpgame.net.command;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;

import stud.mpgame.network.Message;
import stud.mpgame.network.UserObject;

import stud.mpgame.snapshot.ICommand;
import stud.mpgame.snapshot.ISnapshot;

import stud.mpgame.server.game.GameWorld;

/**
 *  UP_KEY_CODE = 0;
 *  RIGHT_KEY_CODE = 1;
 *  DOWN_KEY_CODE = 2;
 *  LEFT_KEY_CODE = 3;
 *
 *  KEY_DOWN = 1;
 *  KEY_UP = 0;
 */
public class ArrowKeyCommand extends UserObject implements ICommand {

    private int time;

    private int keyCode;
    private boolean keyStatus;

    private IoSession session;

    private static String name[] = {"Up", "Right", "Down", "Left"};

    public ArrowKeyCommand() {}

    @Override
    public void readExternal(IoBuffer buffer, Message owner) {
        this.time = buffer.getInt();
        this.keyCode =  buffer.getInt();
        this.keyStatus = (buffer.getInt() > 0);
        this.session = owner.getFirstSession();
    }

    public int getKeyCode() {
        return keyCode;
    }

    public boolean getKeyStatus() {
        return keyStatus;
    }

    public IoSession getSession() {
        return session;
    }

    public int getTime() {
        return time;
    }

    public int getUserID() {
        return (Integer)session.getAttribute("userID");
    }

    @Override
    public String toString() {
        String result = name[keyCode] + " key ";
        result += (keyStatus ? "pressed" : "released");
        return result;
    }

    public void apply(ISnapshot snapshot) {
        //System.out.println("ARROW KEY APPLY" + time);
        ((GameWorld) snapshot).applyArrowKeyCommand(this);
    }

}
